﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Sunboxsoft.Bases.Web
{
    public static class BasesMvcExtension
    {
        public static string BaseUrl = "http://localhost:30837/";

        public static MvcHtmlString MenuTree(this HtmlHelper htmlHelper, List<Models.Menu> menus)
        {
            StringBuilder htmlBuilder = new StringBuilder(@"<ul class=""nav nav-list"">");
            bool hasActiveChild;
            foreach (var menu in menus)
            {
                if (menu.Children != null && menu.Children.Count > 0)
                {
                    OnTreeMenuTree(menu, htmlBuilder, htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url, out hasActiveChild);
                }
                else
                {
                    OnSingleMenuTree(menu, htmlBuilder, htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url, out hasActiveChild);
                }
            }
            htmlBuilder.Append("</ul>");
            return new MvcHtmlString(htmlBuilder.ToString());
        }

        private static void OnSingleMenuTree(Models.Menu menu, StringBuilder htmlBuilder, Uri currentUrl, out bool hasActiveChild)
        {
            if (IsSameUrl(menu.LinkUrl, currentUrl))
            {
                hasActiveChild = true;
                htmlBuilder.AppendFormat(@"<li class=""active""><a href=""{0}""><i class=""icon-dashboard""></i><span class=""menu-text"">{1}</span></a></li>",
                    GetAbsoluteUrl(menu.LinkUrl),
                    menu.Text);
            }
            else
            {
                hasActiveChild = false;
                htmlBuilder.AppendFormat(@"<li><a href=""{0}""><i class=""icon-dashboard""></i><span class=""menu-text"">{1}</span></a></li>",
                    GetAbsoluteUrl(menu.LinkUrl),
                    menu.Text);
            }
        }

        private static void OnTreeMenuTree(Models.Menu menu, StringBuilder htmlBuilder, Uri currentUrl, out bool hasActiveChild)
        {
            hasActiveChild = false;
            bool isActiveOnThisOne = false;
            if (IsSameUrl(menu.LinkUrl, currentUrl))
            {
                isActiveOnThisOne = true;
            }
            else
            {
                isActiveOnThisOne = false;
            }

            StringBuilder childrenHtmlBuilder = new StringBuilder();
            foreach (var subMenu in menu.Children)
            {
                bool hasActiveChildInThis = false;
                if (subMenu.Children != null && subMenu.Children.Count > 0)
                {
                    OnTreeMenuTree(subMenu, childrenHtmlBuilder, currentUrl, out hasActiveChildInThis);
                }
                else
                {
                    OnSingleMenuTree(subMenu, childrenHtmlBuilder, currentUrl, out hasActiveChildInThis);
                }

                if(hasActiveChildInThis)
                {
                    // 有子项目是active的
                    hasActiveChild = true;
                }
            }

            if(hasActiveChild)
            {
                // class open
                htmlBuilder.AppendFormat(@"<li class=""open""><a href=""#"" class=""dropdown-toggle""><i class=""icon-dashboard""></i><span class=""menu-text"">{1}</span><b class=""arrow icon-angle-down""></b></a><ul  class=""submenu"" style=""display: block;"">",
                    GetAbsoluteUrl(menu.LinkUrl),
                    menu.Text);

            }
            else if (isActiveOnThisOne)
            {
                hasActiveChild = true;
                // class active
                htmlBuilder.AppendFormat(@"<li class=""active""><a href=""#"" class=""dropdown-toggle""><i class=""icon-dashboard""></i><span class=""menu-text"">{1}</span><b class=""arrow icon-angle-down""></b></a><ul  class=""submenu"">",
                    GetAbsoluteUrl(menu.LinkUrl),
                    menu.Text);
            }
            else
            {
                hasActiveChild = false;
                htmlBuilder.AppendFormat(@"<li><a href=""#"" class=""dropdown-toggle""><i class=""icon-dashboard""></i><span class=""menu-text"">{1}</span><b class=""arrow icon-angle-down""></b></a><ul  class=""submenu"">",
                    GetAbsoluteUrl(menu.LinkUrl),
                    menu.Text);
            }
            htmlBuilder.Append(childrenHtmlBuilder);
            htmlBuilder.Append("</ul></li>");
        }

        private static bool IsSameUrl(string url1, Uri url2)
        {
            if(string.IsNullOrEmpty(url1))
            {
                return false;
            }

            Uri actualUrl1;
            if(Uri.TryCreate(new Uri(BaseUrl), url1, out actualUrl1) == false)
            {
                return false;
            }
            if(string.Equals(actualUrl1.LocalPath, url2.LocalPath, StringComparison.CurrentCultureIgnoreCase))
            {
                return true;
            }
            return false;
        }

        private static string GetAbsoluteUrl(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return string.Empty;
            }

            Uri actualUrl;
            if (Uri.TryCreate(new Uri(BaseUrl), url, out actualUrl))
            {
                return actualUrl.ToString();
            }
            return url;
        }
    }
}
